package fregat.microchip.usb.device;

import fregat.microchip.usb.device.register;

public class timer0 implements register {
	public enum prescaler {
		_1_256	((byte)0x7),
		_1_128	((byte)0x6),
		_1_64	((byte)0x5),
		_1_32	((byte)0x4),
		_1_8	((byte)0x3),
		_1_4	((byte)0x2),
		_1_2	((byte)0x1),
		_1_1	((byte)0x0);
		
		private byte value;
		
		private prescaler (byte b)
		{
			value = b;
		}
	
		public byte toByte ()
		{
			return value;
		}
	};
	
	private boolean		timerOn;
	private boolean		_8bit;
	private boolean		externalClock;
	private boolean		highToLowClockEdge;
	private boolean		prescalerOn;
	private prescaler	presc;
	
	private int			buffer;
	
	public timer0 ()
	{
		timerOn				= false;
		_8bit				= false;
		externalClock		= false;
		highToLowClockEdge 	= false;
		prescalerOn			= false;
		presc	 			= prescaler._1_2;
	}
	
	public byte toByte ()
	{
		byte b = 0;
		b |= b & (byte)0x7f | (timerOn?0x80:0x0);
		b |= b & (byte)0xbf | (_8bit?0x40:0x0);
		b |= b & (byte)0xdf | (externalClock?0x20:0x0);
		b |= b & (byte)0xef | (highToLowClockEdge?0x10:0x0);
		b |= b & (byte)0xf7 | (prescalerOn?0x8:0x0);
		b |= b & (byte)0xf8 | (presc.toByte() & 0x7); 
		return b;
	}
	
	public void enable ()
	{
		timerOn = true;
	}
	
	public void disable ()
	{
		timerOn = false;
	}
	
	public void setPrescaler (prescaler p)
	{
		presc = p;
	}
	
	public void setBuffer (int buf)
	{
		buffer = buf;
	}
	
	public int getBuffer ()
	{
		return buffer;
	}
}
